﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APPLICATION.Dict.City
{
    public partial class FrmUpdCity : Form
    {        
        private int CodeID;
        private int Level;
        string[] TreeStr;

        public FrmUpdCity(int PCodeID, int PLevel)
        {

            InitializeComponent();
            choiserCountry.ConnectionString = globals.APPCONNSTRING;
            choiserRegion.ConnectionString = globals.APPCONNSTRING;

            DataView DVCountry = new DataView(dataSet1.DtCity);
            DataView DVRegion = new DataView(dataSet1.DtCity);
            DataView DVCity = new DataView(dataSet1.DtCity);
            
            CodeID=PCodeID;
            Level = PLevel;

            bindingSourceCountry.DataSource = DVCountry.ToTable();
            bindingSourceRegion.DataSource = DVRegion.ToTable();
            bindingSourceCity.DataSource = DVCity;
        }

        private void ManageControlsState()
        {            
            switch (Level)
            {
                case 0:
                    panelCity.Visible = false;
                    panelRegion.Visible = false;
                    break;
                case 1:                    
                    panelCity.Visible = false;
                    break;
                case 2:
                     choiserCountry.TextBoxSearch.Enabled = false;                    
                    break;
            }
        }

        private void FormSize()
        {
            switch (Level)
            {
                case 0:
                    this.Height = panelCountry.Height + panel4.Height +35 ;
                    break;
                case 1:
                    this.Height = panelCountry.Height + panelRegion.Height + panel4.Height + 40 ;
                    break;
                default:
                    this.Height = panelCountry.Height + panelRegion.Height + panelCity.Height + panel4.Height +40 ;
                    break;
            }            
        }

        private void FilterData()
        {
            char[] Cpliter = new char[1];
            Cpliter[0]='.';            
            DataRow dr;
            dr=dataSet1.DtCity.Rows.Find(CodeID);
            TreeStr = dr["hrccrt"].ToString().Split(Cpliter, StringSplitOptions.RemoveEmptyEntries);
            switch(Level)
            {
                case 0:
                    this.bindingSourceCountry.Filter = "CodeID=" + TreeStr[0];
                    break;
                case 1:
                    this.bindingSourceCountry.Filter = "CodeID=" + TreeStr[0];
                    this.bindingSourceRegion.Filter = "CodeID=" + TreeStr[1];
                    break;
                case 2:
                    this.bindingSourceCountry.Filter = "CodeID=" + TreeStr[0];                                        
                    this.bindingSourceRegion.Filter = "CodeID=" + TreeStr[1];
                    this.bindingSourceCity.Filter = "CodeID=" + TreeStr[2];
                    break;
            }
        }

        private void FrmNewCity_Load(object sender, EventArgs e)
        {
            
            ManageControlsState();
            FormSize();
            FilterData();
            
            
            choiserCountry.TextBoxSearch.DataBindings.Add("Tag", bindingSourceCountry, "CodeID");
            choiserRegion.TextBoxSearch.DataBindings.Add("Tag", bindingSourceRegion, "CodeID");            
            switch (Level)
            {
                case 0:
                    choiserCountry.TextBoxSearch.DataBindings.Add("Text", bindingSourceCountry, "hName");
                    choiserCountry.ChoiserType = TIMSComponents.ChoiserType.asTextBox;
                    this.Text = "Обновить стран";
                    break;
                case 1:
                    choiserCountry.TextBoxSearch.DataBindings.Add("Text", bindingSourceCountry, "hName");
                    choiserRegion.TextBoxSearch.DataBindings.Add("Text", bindingSourceRegion, "hName");
                    choiserRegion.ChoiserType = TIMSComponents.ChoiserType.asTextBox;
                    choiserCountry.SqlParams = "0, null"  ;                    
                    this.Text = "Обновить регион";
                    break;
                case 2:
                    choiserCountry.TextBoxSearch.DataBindings.Add("Text", bindingSourceCountry, "hName");
                    choiserRegion.TextBoxSearch.DataBindings.Add("Text", bindingSourceRegion, "hName");
                    choiserCountry.TextBoxSearch.Enabled = false;
                    textBoxCity.DataBindings.Add("Text", bindingSourceCity, "hName");
                    choiserRegion.SqlParams = "1," + TreeStr[0];
                    this.Text = "Обновить город";
                    break;
            }
            
        }

        private void textBoxCountry_Validating(object sender, CancelEventArgs e)
        {

        }

        private void textBoxRegion_Validating(object sender, CancelEventArgs e)
        {

        }

        private void textBoxCity_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCity.Text))
            {
                errorProvider1.SetError(textBoxCity, "Plz enter text");
            }
            else
            {
                errorProvider1.SetError(textBoxCity, "");
            }
        }

        private void btnUpdt_Click(object sender, EventArgs e)
        {            
            switch (Level)
            {
                case 0:
                    if (dataSet1.UpdateDataCity(CodeID, choiserCountry.TextBoxSearch.Text, 0))
                    {                        
                        this.Close();
                        MessageBox.Show(this, "Обновлена(es teqsti unda sheicvalos)", "Регион", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case 1:
                    if (dataSet1.UpdateDataCity(CodeID, choiserRegion.TextBoxSearch.Text, Convert.ToInt32(choiserCountry.TextBoxSearch.Tag)))
                    {                        
                        this.Close();
                        MessageBox.Show(this, "Обновлена", "Раион", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case 2:
                    if (dataSet1.UpdateDataCity(CodeID, textBoxCity.Text, Convert.ToInt32(choiserRegion.TextBoxSearch.Tag)))
                    {                        
                        this.Close();
                        MessageBox.Show(this, "Обновлена", "Раион", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
                    }
                    break;
            }
        }

        private void textBoxCity_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void textBoxRegion_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void choiserCountry_Load(object sender, EventArgs e)
        {

        }

        private void panelCountry_Paint(object sender, PaintEventArgs e)
        {

        }

 

    }
}